#! /usr/bin/perl

use strict;

use DBQueries;
use ValidationSubs;
use DBInfoSubs;
use Download;

#now add the relevant information to the database 
#DBQueries::addFile("thomasrantasa", "India_in_transition.txt");
 
#analyze_document(1, "1\.txt");

DBInfoSubs -> update_group_stats("my group");
#DBInfoSubs -> update_user_stats("asdf");

#my @avg_size = DBQueries::getInfoType("thomasrantasa", "My Group", "averageFileSize");
#print "after avg";
#my @avg_word_count = DBQueries::getInfoType("thomasrantasa","My Group", "averageWordCount");
#my @avg_word_length = DBQueries::getInfoType("thomasrantasa","My Group", "averageWordLength");

#print "size obtained from DB:$avg_size[0]\n";
#print "avg word count: $avg_word_count[0]\n";
#print "avg word length: $avg_word_length[0]\n";

#subroutine to analyze a document
#this is passed the file id and name the file has on the system so that it can be easily retreived
sub analyze_document{
	
	#obtain parameters 
	my $file_id = $_[0];
	my $system_file_name = $_[1];
	
	#first obtain the count of each word
	#in order to analyze this file, a hash such that each word is the key to its specific count in the file will be used
	#this means that for each occurence of a word, the hash value indexed by that word will be incremented by one. 
	#declare the hash
	my %word_count = ();

	#declare a list to temporarily hold words of each line
	my @text_line;

	#declare scalar to temporarily hold individual words
	my $word;
	
	#clear any previous info 
	DBQueries::removeInfoID($file_id);
	
	#open the filehandle
	open( FH, "files/$system_file_name");
	
	#loop through entirety of the file and place words in hash
	while ( <FH> ){ 
	
		#split the line into a list of words from that line
		#this splits around all non-word, underscore, new-line, and digit characters
	
		@text_line = split (/\W+|_+|\d+|\n+/,$_);
	
		#now loop through list and make lower case 
		#then add to or increment the count in the hash for each word
		foreach(@text_line){
		
			#make lowercase
			$word = lc($_);
			#if word is already present, increment the counter
			if(exists $word_count{$word}){
				$word_count{$word}++;
			} # end if
			
			#otherwise create a new element with count 1
			else{
				$word_count{$word} = 1;
			} # end else
		
		}#end foreach 
	}#end while

	#now that the hash is created, close the file
	close( FH );
	
	#count the number of unique words
	my $unique_words = keys(%word_count);

	#this will calculate the average length of words with regard to the number of occurences of each word
	#to do this, this code loops through the hash adding the length of each individual word and multiplying it by the number of occurences
	#at the same time a running total of the number of all words will be kept so that the average can be calculated accurately
	my $total_character_count = 0;
	my $total_word_count = 0;

	#loop through hash
	while(my($individual_word, $occurence) = each(%word_count)){
	
		$total_character_count += ($occurence * length($individual_word));
		$total_word_count += $occurence;
	}
	
		print "$total_word_count\n";
	#now the overall average length can be calculated
	my $average_word_length = ($total_character_count / $total_word_count);

	#now sort by occurence
	#using this list, the keys (words) can be accessed directly in the hash
	my @sorted_words = sort{$word_count{$b} <=> $word_count{$a}} keys %word_count;
	
	#we now have a sorted list of all words in teh file by numer of occurences
	#loop through this list and add the information to the database
	
	foreach my $current_sorted_word(@sorted_words){
		
		#add each word to the database
		DBQueries::addInfo( $file_id, "keyword", $current_sorted_word, $word_count{$current_sorted_word});
	}
	
	#at this point all keywords are added to the database
	#add the average word length
	DBQueries::addInfo( $file_id, "avgWordLength", $average_word_length);
	
	#add word count
	DBQueries::addInfo( $file_id, "wordCount", $total_word_count);
	
	#also, obtain the file size
	my $file_size = -s "files/$system_file_name";
	
	#add to database
	DBQueries::addInfo( $file_id, "fileSize", $file_size);
	
	#add number of downloads
	DBQueries::addInfo( $file_id, "downloadCount", 0);
	
}